/*
 * ScrollPaneDelegate.cpp  0.4.0 / Dec 05, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "ScrollPaneDelegate.h"

// ScrollPaneDelegate :: Protected Constructor. --------------------------------

/*!
 * \brief Constructs new ScrollPaneDelegate object.
 *
 * \param env the JNIEnv pointer
 * \param obj the ScrollPane java object
 */
ScrollPaneDelegate::ScrollPaneDelegate(JNIEnv *env, jobject obj)
  : ComponentDelegate(env, obj, new QScrollArea()), m_viewDelegate(0)
{
  // empty constructor body
}

// ScrollPaneDelegate :: Native Calls. -----------------------------------------

/*!
 * \brief Sets view component.
 *
 * \param env the JNIEnv pointer
 * \param viewDelegate the view component delegate
 */
void ScrollPaneDelegate::setView(JNIEnv *env, jlong viewDelegate)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setView, SWING_ENV_ARG, JLONG_ARG(viewDelegate))
  qScrollArea()->takeWidget();
  if (viewDelegate) {
    m_viewDelegate = COMPONENT_CAST(viewDelegate);
    qScrollArea()->setWidget(m_viewDelegate->qWidget());
  } else {
    m_viewDelegate = 0;
  }
}

// ScrollPaneDelegate :: Static Native Calls. ----------------------------------

/*!
 * \brief Returns new instance of the delegate object
 *
 * \param env the JNIEnv pointer
 * \param obj the Swing object
 * 
 * \return new instance of the delegate object
 */
jlong ScrollPaneDelegate::newInstance(JNIEnv *env, jobject obj)
{
  if (JNISwingWrapper::isSwingThread(env)) {
    return reinterpret_cast<jlong>(new ScrollPaneDelegate(env, obj));
  }
  return SwingUtilitiesDelegate::invokeLongAndWait(env, obj, createMID());
}

// ScrollPaneDelegate :: Overridden Member Function. ---------------------------

/*!
 * \inheritDoc
 */
bool ScrollPaneDelegate::filterResizeEvent(QObject *obj, QResizeEvent *e)
{
  bool filtered = ComponentDelegate::filterResizeEvent(obj, e);
  if (m_viewDelegate) {
    // TODO: Include scroll bar dimensions
    QWidget *view = qScrollArea()->widget();
    QSize viewportSize = qScrollArea()->maximumViewportSize();
    QSize preferredViewSize = m_viewDelegate->getPreferredSize();
    view->resize(viewportSize.expandedTo(preferredViewSize));
  }
  return filtered;
}

